package cn.ht.jywrwater.module.ht.controller.admin.waterstation;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.common.pojo.CommonResult;

import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;

import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;

import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;

import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ht.jywrwater.module.ht.controller.admin.waterstation.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterstation.WaterStationDO;
import cn.ht.jywrwater.module.ht.convert.waterstation.WaterStationConvert;
import cn.ht.jywrwater.module.ht.service.waterstation.WaterStationService;

@Tag(name = "管理后台 - 供水站")
@RestController
@RequestMapping("/ht/water-station")
@Validated
public class WaterStationController {

    @Resource
    private WaterStationService waterStationService;

    @PostMapping("/create")
    @Operation(summary = "创建供水站")
    @PreAuthorize("@ss.hasPermission('ht:water-station:create')")
    public CommonResult<Long> createWaterStation(@Valid @RequestBody WaterStationCreateReqVO createReqVO) {
        return success(waterStationService.createWaterStation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供水站")
    @PreAuthorize("@ss.hasPermission('ht:water-station:update')")
    public CommonResult<Boolean> updateWaterStation(@Valid @RequestBody WaterStationUpdateReqVO updateReqVO) {
        waterStationService.updateWaterStation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供水站")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:water-station:delete')")
    public CommonResult<Boolean> deleteWaterStation(@RequestParam("id") Long id) {
        waterStationService.deleteWaterStation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供水站")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:water-station:query')")
    public CommonResult<WaterStationRespVO> getWaterStation(@RequestParam("id") Long id) {
        WaterStationDO waterStation = waterStationService.getWaterStation(id);
        return success(WaterStationConvert.INSTANCE.convert(waterStation));
    }

    @GetMapping("/get-geojson")
    @Operation(summary = "获得供水站geojson")
    @PreAuthorize("@ss.hasPermission('ht:water-station:query')")
    public CommonResult<List<WaterStationGeoRespVO>> getWaterStationGeo() {

        return success(waterStationService.getWaterStationGeo());
    }


    @GetMapping("/list")
    @Operation(summary = "获得供水站列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-station:query')")
    public CommonResult<List<WaterStationRespVO>> getWaterStationList(@RequestParam("ids") Collection<Long> ids) {
        List<WaterStationDO> list = waterStationService.getWaterStationList(ids);
        return success(WaterStationConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供水站分页")
    @PreAuthorize("@ss.hasPermission('ht:water-station:query')")
    public CommonResult<PageResult<WaterStationRespVO>> getWaterStationPage(@Valid WaterStationPageReqVO pageVO) {
        PageResult<WaterStationDO> pageResult = waterStationService.getWaterStationPage(pageVO);
        return success(WaterStationConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供水站 Excel")
    @PreAuthorize("@ss.hasPermission('ht:water-station:export')")
    @OperateLog(type = EXPORT)
    public void exportWaterStationExcel(@Valid WaterStationExportReqVO exportReqVO,
                                        HttpServletResponse response) throws IOException {
        List<WaterStationDO> list = waterStationService.getWaterStationList(exportReqVO);
        // 导出 Excel
        List<WaterStationExcelVO> datas = WaterStationConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "供水站.xls", "数据", WaterStationExcelVO.class, datas);
    }

    @GetMapping("/allStation")
    @Operation(summary = "获得所有供水站")
//    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-station:query')")
    public CommonResult<List<WaterStationRespVO>> getAllWaterStation() {
        List<WaterStationDO> list = waterStationService.getAllWaterStation();
        return success(WaterStationConvert.INSTANCE.convertList(list));
    }

}
